﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EHTWebsite.Code;
using System.Collections.Generic;

namespace EHTWebsiteProject.Administrator
{
    /// <summary>
    /// Author toanvt
    /// CreatedDate 08/12/2011
    /// </summary>
    public partial class AdminBanners : System.Web.UI.Page
    {
        private Int32 phanVungID = -1;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 10;
        private Int32 totalRecord;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch (Exception)
            {
                pageIndex = 1;
            }

            try
            {
                phanVungID = Int32.Parse(Request.QueryString["PhanVungID"].ToString());
            }
            catch (Exception)
            {
                phanVungID = -1;
            }

            if (!Page.IsPostBack)
            {
                LoadPhanVungs();
                LoadDataBanners();
            }
        }

        /// <summary>
        /// Author toanvt
        /// CreateDate 04/02/2012
        /// Description Lấy Videos
        /// </summary>
        private void LoadDataBanners()
        {
            DataSet dsBanners = BannerProvider.GetBannersForBackEnd(phanVungID, pageIndex, pageSize, out totalRecord);

            if (dsBanners.Tables[0].Rows.Count > 0)
            {
                this.rpBanners.DataSource = dsBanners;
                this.rpBanners.DataBind();

                foreach (RepeaterItem item in rpBanners.Items)
                {
                    CheckBox ckPublish = (CheckBox)item.FindControl("ckPublish");

                    //Hiện biểu tượng tương ứng với trạng thái Publish
                    if (ckPublish.Checked == true)
                    {
                        ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/btncheck.png";
                    }
                    else
                    {
                        ((ImageButton)item.FindControl("btnPublish")).ImageUrl = "Templates/Images/unpublish.jpg";
                    }
                }

                PaggingControl1.PageURL = "AdminBanners.aspx?PhanVungID=" + phanVungID.ToString() ;
                PaggingControl1.PageIndex = pageIndex;
                PaggingControl1.TotalRecord = totalRecord;
            }
        }

        /// <summary>
        /// Author : toanvt
        /// CreatedDate : 06/02/2012
        /// Description : Lấy danh sách Phân Vùng Banner
        /// </summary>
        private void LoadPhanVungs()
        {
            DataSet dsBanners = BannerProvider.GetPhanVungBanners();

            this.ddlPhanVung.DataTextField = "TitleOrName";
            this.ddlPhanVung.DataValueField = "PhanVungID";
            this.ddlPhanVung.DataSource = dsBanners;
            this.ddlPhanVung.DataBind();
             
            this.ddlPhanVung.Items.Insert(0, new ListItem("Tất cả", "-1"));
            this.ddlPhanVung.SelectedValue = phanVungID.ToString();
        }

        protected void rpBanners_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            String cmdName = e.CommandName;
            Int32 bannerID = -1;

            try
            {
                bannerID = Int32.Parse(e.CommandArgument.ToString());
            }
            catch (Exception ex)
            {
            }

            if (cmdName == "delete1")
            {
                BannerProvider.DeleteBannerByBannerID(bannerID);
                LoadDataBanners();
            }
            else if (cmdName == "publish")
            {
                BannerProvider.ChangePublishByBannerID(bannerID);
                LoadDataBanners();
            }
            else if (cmdName == "change_number_of_order")
            {
                try
                {
                    Int32 numberOfOrder = Int32.Parse(((TextBox)e.Item.FindControl("txtNumberOfOrder")).Text.Trim());
                    BannerProvider.UpdateNumberOfOrderByBannerID(bannerID, numberOfOrder);
                }
                catch (Exception ex)
                {

                }

                LoadDataBanners();

            }
            else if (cmdName == "save_all")
            {
                foreach (RepeaterItem item in rpBanners.Items)
                {
                    Int32 numberOfOrder = Int32.Parse(((TextBox)item.FindControl("txtNumberOfOrder")).Text.Trim());
                    bannerID = Int32.Parse(((Literal)item.FindControl("ltrBannerID")).Text.Trim());
                    BannerProvider.UpdateNumberOfOrderByBannerID(bannerID, numberOfOrder);
                }

                LoadDataBanners();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminBannerIU.aspx?PhanVungID=" + phanVungID.ToString());
        }

        protected void ddlPhanVung_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("AdminBanners.aspx?PhanVungID=" + this.ddlPhanVung.SelectedValue);
        }
    }
}